/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for IFrameHost.
	/// </summary>
	public class IFrameHost : ResLinkAdminPage
	{
		#region Child Controls

		protected SectionEditControl SectionEdit;
		protected CommunityServer.Controls.Style Style1;
		protected CommunityServer.Controls.Style Style2;
		protected CommunityServer.Controls.Style Style3;
		protected Script Script1;
		protected Script Script2;
		protected Head Head1;
		protected LinkButton Save;
		protected MPContent bcr;
		protected StatusMessage Status;
		#endregion

		private void Page_Load(object sender, EventArgs e)
		{

			Status.Visible = false;

			int sectionID = CSContext.Current.GetIntFromQueryString("SectionID", 0);

			// Determine whether to display the SectionEdit control or GroupEdit control
			if (sectionID > 0)
			{
				SectionEdit.Visible = true;
				SectionEdit.SectionID = sectionID;
	
				SectionEdit.Section = ResLinkSection.GetSection(sectionID);

				Save.Visible = true;
			}
			else
			{
				SectionEdit.Visible = false;
				Save.Visible = false;
			}
			if ( !Page.IsPostBack ) 
			{
				// Display a message if a Group or Section was just added/deleted
				if (CSContext.Current.GetIntFromQueryString("SectionDeleted", -1) > 0)
					DisplayStatusMessage(true, "CP_ResLink_SectionEdit_StatusDeleteSuccess");

			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			Save.Click += new EventHandler(Save_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private void DisplayStatusMessage(bool success, string resourceName)
		{
			Status.Success = success;
			Status.Visible = true;
			Status.ResourceName = resourceName;
		}

		private void Save_Click(object sender, EventArgs e)
		{
			bool redirect = false;
			string script = @"
					<script language=""javascript"">
						{0};
					</script>";
		
			if(!Page.IsValid)
				return;

			if(SectionEdit.Visible)
			{
				redirect = SectionEdit.Save();
				string command = string.Format("window.top.updateSectionNode('{0}', '{1}')", SectionEdit.Section.Name, SectionEdit.Section.IsActive); 
				script = string.Format(script, command); 
			}
		
			if(redirect)
			{
				DisplayStatusMessage(true, "CP_ResLink_SaveSuccessful");

				Page.RegisterStartupScript("Redirect",script) ;
			}

		}
	}
}
